//
//  XHBuyBackBounceViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/20.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHBuyBackBounceViewController.h"
#import "XHTabBarController.h"

#import "AppDelegate.h"
#import "Services.h"


#define SCREEN_HEIGHT                   ([UIScreen mainScreen].bounds.size.height)
#define SCREEN_WIDTH                    ([UIScreen mainScreen].bounds.size.width)

@interface XHBuyBackBounceViewController ()
@property (weak, nonatomic) IBOutlet UIView *ui_bgView;
@property (weak, nonatomic) IBOutlet UILabel *ui_balance;
@property (weak, nonatomic) IBOutlet UILabel *ui_valueAdd;
@property (weak, nonatomic) IBOutlet UILabel *ui_total;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UIButton *buyBackButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *la_bgViewHeight;
@property (strong, nonatomic) NSTimer *timer;

@end

@implementation XHBuyBackBounceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ui_bgView.layer.masksToBounds = YES;
    self.ui_bgView.layer.cornerRadius = 8;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self.navigationItem setHidesBackButton:YES];
//    [self setHidesBottomBarWhenPushed:YES];
    [self goAnimation];
    [self createUI];
}

- (void)createUI
{
    self.ui_total.text = [NSString stringWithFormat:@"￥%.2f",[self.buyBackDic[@"principal"] doubleValue]];
    self.ui_valueAdd.text = [NSString stringWithFormat:@"+%.2f",[self.buyBackDic[@"cumulativeInterest"] doubleValue]];
    self.ui_balance.text = [NSString stringWithFormat:@"+%.2f",[self.buyBackDic[@"principal"] doubleValue]];
}

//阻尼动画
- (void)goAnimation
{
    CGPoint startPoint = CGPointMake((SCREEN_WIDTH-270)/2, -(SCREEN_HEIGHT-200)/2);
    self.ui_bgView.layer.position=startPoint;
    [UIView animateWithDuration:.8 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGPoint startPoint = CGPointMake((SCREEN_WIDTH-270)/2, (SCREEN_HEIGHT-200)/2 +200);
        self.ui_bgView.layer.position=startPoint;
        self.la_bgViewHeight.constant = (SCREEN_HEIGHT-270)/2;
        
    } completion:^(BOOL finished) {
        
    }];
}


- (IBAction)cancelAction:(id)sender
{
    [UIView animateWithDuration:0.35 animations:^{
        
        self.la_bgViewHeight.constant = -350;
        [self.view layoutIfNeeded];
    }];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.35
                                                  target:self selector:@selector(cancelViewController) userInfo:nil repeats:YES];

}
- (IBAction)buyBackAction:(id)sender
{
    self.buyBackButton.userInteractionEnabled = NO;
    [ServiceManager orderSnBuyBackFinancialBenefitsWithOrderSn:self.buyBackDic[@"orderSn"] success:^(NSDictionary *data) {
        [UIView animateWithDuration:0.35 animations:^{
            
            self.la_bgViewHeight.constant = SCREEN_HEIGHT;
            [self.view layoutIfNeeded];
        }];
        self.buyBackButton.userInteractionEnabled = YES;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.35
                                                      target:self selector:@selector(cancelViewController) userInfo:nil repeats:YES];
        [PromptAction showSuccessPrompt:@"回购成功"];
        AppDelegate * delegate = [UIApplication sharedApplication].delegate;
        [delegate.tabBarController myEarningViewController];
        [self.navigationController popToRootViewControllerAnimated:YES];

    } failure:^(NSDictionary *data) {
        [PromptAction showSuccessPrompt:@"回购失败"];
    }];
}

- (void)cancelViewController
{
    [self dismissViewControllerAnimated:NO completion:^{
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
